﻿// /////////////////////////////////// 
//  
//  
//      WiseLink CCF2.0 
//     (c)Copyright 2001-2010 WiseLink Xiaojun (David) Chen 
//        Authored and Owned Xiaojun (David) Chen 
//        All Right Reserved
//  
// /////////////////////////////////// 

using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq.Expressions;
using System.Windows.Input;
using System.Collections.Generic;

namespace CCF20Model.Common.Infrastructure
{
    
        [Serializable]
    public abstract class ObservableObject : INotifyPropertyChanged, IDataErrorInfo 
        {


            RelayCommand _closeCommand;

            public ICommand CloseCommand
            {
                get 
                { 
                    if (_closeCommand == null)
                    { 
                        _closeCommand = new RelayCommand( () => Close());
                    }
                    return _closeCommand; 
                }
            }
            public event Action RequestClose;

            protected void Close()
            {
                if (RequestClose != null) 
                { 
                    RequestClose();
                }
            }

            [field: NonSerialized]
            public event PropertyChangedEventHandler PropertyChanged;

            protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
            {
                var handler = this.PropertyChanged;
                if (handler != null)
                {
                    handler(this, e);
                }
            }

            protected void RaisePropertyChanged<T>(Expression<Func<T>> propertyExpresssion)
            {
                var propertyName = PropertySupport.ExtractPropertyName(propertyExpresssion);
                this.RaisePropertyChanged(propertyName);
            }

            protected void RaisePropertyChanged(String propertyName)
            {
                VerifyPropertyName(propertyName);
                OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
            }

            
            public void VerifyPropertyName(String propertyName)
            {
                // verify that the property name matches a real,  
                // public, instance property on this Object.
                if (TypeDescriptor.GetProperties(this)[propertyName] == null)
                {
                    Debug.Fail("Invalid property name: " + propertyName);
                }
            }

            #region IDataErrorInfo Members

            private Dictionary<string, string> m_validationErrors = new Dictionary<string, string>();

            protected void AddError(string columnName, string msg)
            {
                if (!m_validationErrors.ContainsKey(columnName))
                {
                    m_validationErrors.Add(columnName, msg);
                }

            }

            protected void RemoveError(string columnName)
            {
                if (m_validationErrors.ContainsKey(columnName))
                {
                    m_validationErrors.Remove(columnName);
                }
            }

            public string Error
            {
                get
                {
                    if (m_validationErrors.Count > 0)
                    {
                        return "Customer data is invalid";
                    }
                    else return null;
                }
            }

            public string this[string columnName]
            {
                get
                {
                    if (m_validationErrors.ContainsKey(columnName))
                    {
                        return m_validationErrors[columnName];
                    }
                    else return null;
                }
            }

            #endregion
        }
    
}
